// components/camp-goods/camp-goods.js
const {
  baseURL
} = require("../../utils/request")
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    campGoodsStatus: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    baseURL,
    campGoodsStatus: false,
    addList: [], //整体数据
    campId: "",
    goodsImg: "",
    goodsImgs: [],
    fileList: [],
    GoodsName: "",
    memberPrice: "",
    goodsPrice: "",
    goodsDesc: ""
  },
  lifetimes: {
    ready() {

    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //获取图片
    getUploadList(e) {
      console.log(e)
      this.setData({
        goodsImg: e.detail
      })
      console.log('上传的图片：', this.data.goodsImg);
    },
    //重置数据
    resetData() {
      this.setData({
        // conditionList: result
        goodsName: "",
        memberPrice: "",
        goodsPrice: "",
        goodsDesc: ""
      })
    },
    //添加虚拟数据
    addData(e) {
      const object = {
        goodsName: e.detail.value.goodsName,
        goodsDesc: e.detail.value.goodsDesc,
        memberPrice: Number(e.detail.value?.memberPrice),
        goodsPrice: Number(e.detail.value.goodsPrice),
        goodsImg: this.data.goodsImg.join(","),
      }
      console.log(object);
      var isEmpty = true;
      Object.keys(object).forEach(function (x) {
        if (object[x] === null || object[x] === "") {
          isEmpty = false;
        }
      });
      if (isEmpty) {
        this.data.addList.push({
          goodsName: e.detail.value.goodsName,
          goodsDesc: e.detail.value.goodsDesc,
          memberPrice: Number(e.detail.value?.memberPrice),
          goodsPrice: Number(e.detail.value.goodsPrice),
          goodsImg: this.data.goodsImg.join(","),
        })
        this.resetData();
        this.onClose();
        this.triggerEvent("sendData", this.data.addList)
      } else {
        wx.showToast({
          title: "信息未填写完整",
          icon: "error",
          duration: 1500
        })
      }
      console.log(this.data.addList);
    },
    onClose() {
      this.resetData()
      this.setData({
        campGoodsStatus: false
      })
    },
  }
})